package com.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.Seconds;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.lang.management.ManagementFactory;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Date2Utils {
	/**
	 * 1秒 java以毫秒为单位
	 */
	public static final long SECOND = 1000;
	/**
	 * 1分钟
	 */
	public static final long MINUTE = SECOND * 60;
	/**
	 * 1小时
	 */
	public static final long HOUR = MINUTE * 60;
	/**
	 * 1天
	 */
	public static final long DAY = HOUR * 24;
	/**
	 * 1周
	 */
	public static final long WEEK = DAY * 7;
	/** 1年 */
	public static final long YEAR = DAY * 365;
	/** 默认日期格式 */
	public static final String FORTER_DATE = "yyyy-MM-dd";
	/** 默认时间格式 */
	public static final String FORMAT_TIME = "HH:mm:ss";
	/** 默认日期时间格式 */
	public static final String FORTER_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
	/** 时间格式:yyyyMMddHHmmss */
	public static final String YYYYMMDD = "yyyyMMdd";
	public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
	public static final String YYYYMMDDHHMMSS_SSS = "yyyyMMddHHmmss.SSS";

	public static final Date getNowDate() {
		DateTime dateTime = new DateTime();
		return dateTime.toDate();
	}

	public static final Date getNowDateByDays(int days) {
		DateTime dateTime = new DateTime();
		return dateTime.plusDays(days).toDate();
	}

	 
	public static final Date getDateByDays(Date date, int days) {
		DateTime dateTime = new DateTime(date);
		return dateTime.plusDays(days).toDate();
	}

	 
	public static final String getByDays(String dateStr, int days) {
		Date inputDate = getStrToDate(dateStr, FORTER_DATE);
		Date intervalDate = getDateByDays(inputDate, days);
		return getDateToStr(intervalDate, FORTER_DATE);
	}

	public static final String getNowDateStr() {
		return getDateToStr(null, FORTER_DATE_TIME);
	}

	public static final String getNowTimeStr() {
		return getDateToStr(null, FORMAT_TIME);
	}

	public static final String getNowDateStr(String format) {
		return getDateToStr(null, format);
	}

	public static final String getNowDateByDays(String format, int days) {
		DateTime dateTime = new DateTime();
		return dateTime.plusDays(days).toString(format);
	}

	public static final String getNowDateByYears(String format, int years) {
		DateTime dateTime = new DateTime();
		return dateTime.plusYears(years).toString(format);
	}

	public static final String getDateToStr(Date date, String format) {
		DateTime dateTime = null;
		if (null == date) {
			dateTime = new DateTime();
		} else {
			dateTime = new DateTime(date);
		}
		if (StringUtils.isBlank(format)) {
			format = FORTER_DATE_TIME;
		}
		return dateTime.toString(format);
	}

	public static final Date getStrToDate(String dateStr, String format) {
		if (StringUtils.isBlank(dateStr)) {
			return null;
		}
		if (StringUtils.isBlank(format)) {
			format = FORTER_DATE_TIME;
		}
		DateTimeFormatter formatter = DateTimeFormat.forPattern(format);
		DateTime dateTime = DateTime.parse(dateStr, formatter);
		return dateTime.toDate();
	}

	/**
	 * 计算区间天数
	 * 
	 * @Description:
	 * @param beginDate yyyy-MM-dd HH:mm:ss
	 * @param endDate   yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static final int getDays(String beginDate, String endDate) {
		beginDate = formatDataStr(beginDate);
		endDate = formatDataStr(endDate);
		DateTime begin = new DateTime(beginDate);
		DateTime end = new DateTime(endDate);
		// 计算区间天数
		Period period = new Period(begin, end, PeriodType.days());
		int days = period.getDays();
		return days;
	}

	/**
	 * 计算区间秒
	 * 
	 * @Description:
	 * @param beginDate yyyy-MM-dd HH:mm:ss
	 * @param endDate   yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static final int getSeconds(String beginDate, String endDate) {
		Date date1 = getStrToDate(beginDate, FORTER_DATE_TIME);
		Date date2 = getStrToDate(endDate, FORTER_DATE_TIME);
		DateTime begin = new DateTime(date1);
		DateTime end = new DateTime(date2);
		return Seconds.secondsBetween(begin, end).getSeconds();
		// 计算区间天数
//		Period period = new Period(begin, end, PeriodType.seconds());
//		return period.getSeconds();
	}

	/**
	 * 字符日期格式化:默认 yyyy-MM-dd HH:mm:ss 转换成 yyyy-MM-dd
	 * 
	 * @param dateStr
	 * @return
	 */
	public static String formatDataStr(String dateStr) {
		Date date = getStrToDate(dateStr, FORTER_DATE_TIME);
		DateTime dateTime = new DateTime(date);
		return dateTime.toString(FORTER_DATE);
	}

	/**
	 * 字符日期格式化:默认 yyyy-MM-dd 转换成 yyyy-MM-dd HH:mm:ss,默认时间获取当前时间
	 * 
	 * @Title: formatDateToDateTime
	 * @Description:
	 * @date:2019年4月26日
	 * @author:
	 * @param dateStr
	 * @return
	 */
	public static String formatDateToDateTime(String dateStr) {
		DateTime nowDateTime = new DateTime();
		String nowTime = nowDateTime.toString(FORMAT_TIME);
//		Date date = getStrToDate(dateStr, FORTER_DATE);
//		DateTime dateTime = new DateTime(date);
//		System.out.println(dateTime.getYear());
//		System.out.println(dateTime.getMonthOfYear());
//		System.out.println(dateTime.getDayOfMonth());
//		DateTime nowDateTime = new DateTime();
//		System.out.println(nowDateTime.getHourOfDay());
//		System.out.println(nowDateTime.getMinuteOfHour());
//		System.out.println(nowDateTime.getSecondOfMinute());
//
//		DateTime dateTime2 = new DateTime(dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfMonth(),
//				nowDateTime.getHourOfDay(), nowDateTime.getMinuteOfHour(), nowDateTime.getSecondOfMinute());
		return dateStr + " " + nowTime;
	}

	/**
	 * 是否大于当前时间
	 * 
	 * @param dataTimeStr
	 * @return
	 */
	public static boolean isAfterNow(String dataTimeStr) {
		DateTime dateTime = DateTime.parse(dataTimeStr, DateTimeFormat.forPattern(FORTER_DATE_TIME));
		return dateTime.isAfterNow();
	}

	/**
	 * 是否小于当前时间
	 * 
	 * @param dataTimeStr
	 * @return
	 */
	public static boolean isBeforeNow(String dataTimeStr) {
		DateTime dateTime = DateTime.parse(dataTimeStr, DateTimeFormat.forPattern(FORTER_DATE_TIME));
		return dateTime.isBeforeNow();

	}

	public static String getDateTime(long currentTimeMillis) {
		DateTime dateTime = new DateTime(currentTimeMillis);
		return dateTime.toString(FORTER_DATE_TIME);
	}

	/**
	 * 获取星期
	 * 
	 * @Title: getNowWeekName
	 * @Description:
	 * @date:2019年2月19日
	 * @author:
	 * @return
	 */
	public static String getNowWeekName() {
		DateTime dateTime = new DateTime();
		return dateTime.dayOfWeek().getAsShortText(Locale.CHINESE);
	}

	/**
	 * 获取与当前时间间隔时间(毫秒)
	 * 
	 * @Title: getNowMillisecond
	 * @Description:
	 * @date:2019年3月18日
	 * @author:
	 * @param millisecond 间隔时间：毫秒
	 * @return
	 */
	public static long getNowMillisecond(long millisecond) {
		long nowMillisecond = System.currentTimeMillis();
		return nowMillisecond + millisecond;
	}

	/**
	 * 判断时间格式
	 * 
	 * @Title: isValidDate
	 * @Description:
	 * @date:2019年4月26日
	 * @author:
	 * @param dateStr
	 * @return
	 */
	public static boolean isValidDate(String dateStr, String format) {
		if (StringUtils.isBlank(dateStr)) {
			return false;
		}
		if (StringUtils.isBlank(format)) {
			format = FORTER_DATE_TIME;
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		try {
			Date date = simpleDateFormat.parse(dateStr);
			return dateStr.equals(simpleDateFormat.format(date));
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 检查日期的先后顺序 startDate <= endDate
	 * 
	 * @param startDate yyyy-MM-dd
	 * @param endDate   yyyy-MM-dd
	 * @return
	 */
	public static boolean isAfterEndDate(String startDate, String endDate) {
		Date sDate = getStrToDate(startDate, FORTER_DATE);
		Date eDate = getStrToDate(endDate, FORTER_DATE);
		return sDate.getTime() <= eDate.getTime();
	}

	/**
	 * 
	 * @Title: getGabTime
	 * @Description:
	 * @date:2019年8月21日
	 * @author:
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	public static final String getGabTime(String beginDate, String endDate) {
		Date date1 = getStrToDate(beginDate, FORTER_DATE_TIME);
		Date date2 = getStrToDate(endDate, FORTER_DATE_TIME);
		DateTime begin = new DateTime(date1);
		DateTime end = new DateTime(date2);
		Interval interval = new Interval(begin, end);
		Period period = interval.toPeriod();
		if (period.getDays() > 0) {
			return String.format("%d天%d时%d分%d秒", period.getDays(), period.getHours(), period.getMinutes(),
					period.getSeconds());
		}
		return String.format("%d时%d分%d秒", period.getHours(), period.getMinutes(), period.getSeconds());
	}

	/**
	 * 秒数转转成 HH:mm:ss
	 * 
	 * @param secondTime
	 * @return
	 */
	public static final String secondTransferTime(int secondTime) {
		int daySecond = 86400;// 每天 86400 秒
		String time = "";
		String secondStr = String.format("%02d", secondTime % 60);
		String minuteStr = String.format("%02d", (secondTime / 60) % 60);
		String hourStr = String.format("%02d", (secondTime / 3600) % 24);
		if (secondTime < daySecond) {
			time = String.format("%s:%s:%s", hourStr, minuteStr, secondStr);
		} else {
			String dayStr = String.format("%04d", secondTime / daySecond);
			time = String.format("%s天%s:%s:%s", NumberUtils.toInt(dayStr, 0), hourStr, minuteStr, secondStr);
		}
		return time;
	}

	/**
	 * 获取服务器启动时间
	 * 
	 * @return
	 */
	public static Date getServerStartDate() {
		long currentTimeMillis = ManagementFactory.getRuntimeMXBean().getStartTime();
//		return getDateTime(currentTimeMillis);
		return new Date(currentTimeMillis);
	}

	/**
	 * 计算两个时间差
	 */
	public static String getDatePoor(Date endDate, Date nowDate) {
		// long ns = 1000;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少天
		long day = diff / DAY;
		// 计算差多少小时
		long hour = diff % DAY / HOUR;
		// 计算差多少分钟
		long min = diff % DAY % HOUR / MINUTE;
		// 计算差多少秒//输出结果
		// long sec = diff % nd % nh % nm / ns;
		return day + "天" + hour + "小时" + min + "分钟";
	}

	public static void main(String[] args) {
		/*
		 * DateTimeFormatter formatter = DateTimeFormat.forPattern(FORTER_DATE);
		 * DateTime dateTime = DateTime.parse("2019-04-15", formatter); Date date =
		 * getDateByDays(dateTime.toDate(), 1); System.out.println(getDateToStr(date,
		 * FORTER_DATE_TIME));
		 */

		System.out.println(getSeconds("2019-05-23 13:27:28", "2019-05-23 17:32:34"));
		String beginDate = Date2Utils.getNowDateStr();
		String endDate = Date2Utils.getNowDateByDays(Date2Utils.FORTER_DATE_TIME, 7);
		System.out.println("beginDate:" + beginDate);
		System.out.println("endDate:" + endDate);
		System.out.println(getGabTime("2019-08-19 16:50:07", "2019-08-21 16:00:19"));
		System.out.println(getByDays("2019-08-26", 1));
		System.out.println(Date2Utils.getDateTime(1566822285 * Date2Utils.SECOND));

		DateTime nowDateTime = new DateTime();
		String nowTime = nowDateTime.toString(FORMAT_TIME);
		System.out.println(nowTime);
		System.out.println(nowDateTime.getYear());
		System.out.println(nowDateTime.getMonthOfYear());
		System.out.println(nowDateTime.getDayOfMonth());
		System.out.println(nowDateTime.getHourOfDay());
		System.out.println(nowDateTime.getMinuteOfHour());
		System.out.println(nowDateTime.getSecondOfMinute());
		System.out.println(getNowDateStr("yyyy-MM-ddTHH:mm:ss"));
	}
}
